#ifndef _THREAD_CPP_
#define _THREAD_CPP_

#include "Share.h"

Thread::Thread( )
	: thread_func_( 0 )
	, thread_id_( 0 )
{
}

Thread::~Thread( void )
{
	if( thread_id_ )
		join( );

	if( thread_func_ )
		delete thread_func_;
}

void Thread::start( )
{
#ifndef WIN32
	pthread_create( &thread_id_, 0, warpped_fun, this );
#else
	thread_id_ = CreateThread( 0, 0, warpped_fun, this, 0, 0 );
#endif
}

void Thread::join( )
{
#ifndef WIN32
	pthread_join( thread_id_, 0 );
#else
	WaitForSingleObject( thread_id_, INFINITE );
	CloseHandle( thread_id_ );
#endif
	thread_id_ = 0;
}

THRFUNC_RETURN Thread::warpped_fun( void * param )
{
	Thread *t = ( Thread* ) param;
	t->thread_func_->invoke( );
	return THRFUNC_RETURN( 0 );
}

#endif
